package cn.itcast.nems.listener.studentrecord;

import cn.hutool.core.lang.Assert;
import cn.itcast.nems.common.tenant.TenantInfo;
import cn.itcast.nems.common.tenant.TenantInfoHolder;
import cn.itcast.nems.listener.NemsMessageListener;
import cn.itcast.nems.studentrecord.dto.studentrecord.StudentRecordBalanceAmountDueMessageDTO;
import cn.itcast.nems.studentrecord.service.ClazzEnrollmentCountService;
import com.alibaba.fastjson.JSON;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
/**
 * <p>
 * 监听剩余应付金额变动消息
 * </p>
 *
 * @author zhouwei
 * @since 2023/12/22 11:07 AM
 **/
@Component
@RequiredArgsConstructor
@Slf4j
public class StudentRecordBalanceAmountDueMessageListener implements NemsMessageListener {
    private final static String TagName  =  "TAG_STUDENT_RECORD_BALANCE_AMOUNT_DUE" ;
    private final ClazzEnrollmentCountService clazzEnrollmentCountService ;

    @Override
    public String getTagName() {
        return TagName;
    }

    @Override
    public void processMessage(String body) {
        // 剩余应付金额变动消息
        StudentRecordBalanceAmountDueMessageDTO balanceAmountDueMessageDTO = JSON.parseObject(body, StudentRecordBalanceAmountDueMessageDTO.class);
        log.info("【监听剩余应付金额变动消息】消息体详情：{}" ,  JSON.toJSONString(balanceAmountDueMessageDTO));
        Assert.notNull(balanceAmountDueMessageDTO, "【消息监听】tag: TAG_STUDENT_RECORD_BALANCE_AMOUNT_DUE,消息体中业务数据实体为空");
        Assert.notBlank(balanceAmountDueMessageDTO.getStudentRecordId(), "【消息监听】tag: TAG_STUDENT_RECORD_BALANCE_AMOUNT_DUE,学籍ID为空");
        TenantInfoHolder.setTenantInfo(new TenantInfo(balanceAmountDueMessageDTO.getTenantId(), balanceAmountDueMessageDTO.getSubtenantId()));
        clazzEnrollmentCountService.calculatePaidCount(balanceAmountDueMessageDTO.getStudentRecordId());
    }
}
